package com.codefactory.generator.config;

import com.codefactory.generator.method.ClassNameMethod;
import com.codefactory.generator.method.UpperCaseMethod;
import com.codefactory.generator.method.VarNameMethod;
import freemarker.template.TemplateExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FreemarkerConfig {
    @Value("${freemarker.template-loader-path:classpath:templates/}")
    private String templateLoaderPath;

    @Value("${freemarker.charset:UTF-8}")
    private String charset;

    @Bean
    public freemarker.template.Configuration configuration() {
        freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_23);
        //cfg.setDirectoryForTemplateLoading(new File(templateLoaderPath));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        cfg.setSharedVariable("upper", new UpperCaseMethod());
        cfg.setSharedVariable("varName", new VarNameMethod());
        cfg.setSharedVariable("className", new ClassNameMethod());
        //${upper("hello world")}  // 返回 "HELLO WORLD"
        return cfg;
    }
}